﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

/// <summary>
/// 控制台命令管理
/// </summary>
namespace Common.ConsoleSysMessages
{
    /// <summary>
    /// 命令属性
    /// </summary>
    public class Command : Attribute
    {
        /// <summary>
        /// 命令名
        /// </summary>
        private string m_CommandName;
        /// <summary>
        /// 错误说明
        /// </summary>
        private string m_Description;
        public Command(string Name, string Des)
        {
            m_CommandName = Name;
            m_Description = Des;
        }

        public string GetName()
        {
            return m_CommandName;
        }

        public string GetDes()
        {
            return m_Description;
        }
    }

    public class CommandBase
    {
        public object m_Object;
        public MethodInfo m_MethodInfo;
        public string m_Des;

        public CommandBase(object Object, MethodInfo meth, string Des)
        {
            m_Object = Object;
            m_MethodInfo = meth;
            m_Des = Des;
        }
    }



    /// <summary>
    /// 控制台命令管理者
    /// </summary>
    public class ConsoleCommandManager
    {
        public Dictionary<string, CommandBase> m_CommandDictionary = new Dictionary<string, CommandBase>();

        private static ConsoleCommandManager m_instance;

        public static ConsoleCommandManager GetInstance()
        {
            if (m_instance == null)
            {
                m_instance = new ConsoleCommandManager();
            }
            return m_instance;
        }

        /// <summary>
        /// 注册命令函数
        /// </summary>
        /// <param name="pLuaAPIClass"></param>
        public void RegisterCommand(Object pLuaAPIClass)
        {
            foreach (MethodInfo mInfo in pLuaAPIClass.GetType().GetMethods())
            {
                foreach (Attribute attr in Attribute.GetCustomAttributes(mInfo))
                {
                    //LuaInterface.Lua
                    if (attr is Command)
                    {
                        string LuaFunctionName = (attr as Command).GetName();
                        Console.WriteLine("注册控制台命令:{0}", LuaFunctionName);

                        m_CommandDictionary.Add(LuaFunctionName, new CommandBase(pLuaAPIClass, pLuaAPIClass.GetType().GetMethod(LuaFunctionName), (attr as Command).GetDes()));
                    }
                }
            }
        }


        /// <summary>
        /// 调用
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="args"></param>
        public void Call(string Name, object[] args)
        {
            try
            {
                //判断是否带参数
                if (args.Length > 0)
                {

                    object[] ObjArgs = new object[1] { args };
                    if (bool.Parse(m_CommandDictionary[Name].m_MethodInfo.Invoke(m_CommandDictionary[Name].m_Object, ObjArgs).ToString()) == false)
                    {
                        System.Console.WriteLine(m_CommandDictionary[Name].m_Des);
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("命令错误:{0}", e.ToString());
            }
        }




    }
}
